home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / LANG / PROLOG / BP330 / !BinPro330 / progs / knight < prev    next >
Text File  |  1993-11-16  |  1KB  |  69 lines

  1. /*----------------------------------------------------------------------------
  2. Program:  Knight's Tour
  3. Author:   E. Tick
  4. Date:     September 10 1989
  5. Notes:
  6. % adapted and optimized for BinProlog: Paul Tarau, August 1992
  7. % changed semantics: the knight has to cover the board completely
  8.  
  9. 1. To run:
  10.     ?- go(N,T,S).    
  11. for input N (side of board), output T is execution time, and S is the number 
  12. of solutions.  Usually we run go(5,X,Y).
  13.  
  14. ----------------------------------------------------------------------------
  15. :- sequential.
  16. :- parallel move/2.
  17. */
  18.  
  19. p:-[knight].
  20.  
  21.  
  22. go:-go(5).
  23.  
  24. go(N):-time(_),init(N,M),knight(M,1,1),!,time(T),
  25.     write(time=T),nl,statistics,show(N).
  26.  
  27. init(N,_):-
  28.     for(I,1,N),
  29.         for(J,1,N),
  30.             bb_def(I,J,NewVar),
  31.     fail.
  32. init(N,M):-
  33.     M is N*N.
  34.  
  35. knight(0,_,_) :- !.
  36. knight(K,A,B) :-
  37.         K1 is K-1,
  38.         val(A,B,K),
  39.         move(Dx,Dy),
  40.         step(K1,A,B,Dx,Dy).
  41.  
  42. step(K1,A,B,Dx,Dy):-
  43.     C is A + Dx,
  44.     D is B + Dy,
  45.     knight(K1,C,D).
  46.  
  47. % wam.c: strange bug when cells are not cast to (int) in arith ops!!!
  48. show(N):-
  49.     cwrite('The Board'),nl,
  50.     for(I,1,N),
  51.         nl,
  52.         for(J,1,N),
  53.             val(I,J,V),
  54.             X is 1-V // 10,
  55.             cwrite(' '),tab(X),cwrite(V),
  56.     fail.
  57. show(_):-nl.
  58.  
  59. move( 2, 1).
  60. move( 2,-1).
  61. move(-2, 1).
  62. move(-2,-1).
  63. move( 1, 2).
  64. move(-1, 2).
  65. move( 1,-2).
  66. move(-1,-2).
  67.  
  68. time(T) :- statistics(runtime,[_,T]).
  69.